package study.test.test.blog;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import common.utils.ClassUtil;
import common.utils.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 批量增加博客的访问量
 * 
 * @ 目前博客的规则应该是每分钟增加1
 * 
 * @ClassName BlogViewsAdd
 * @author <a href="892042158@qq.com" target="_blank">于国帅</a>
 * @date 2018年9月6日 上午11:06:26
 *
 */
@Slf4j
public class BlogViewsAdd {
    protected static Timer clearTimer;
    protected static Long period;

    public static void main(String[] args) {
        clearTimer = new Timer();
        period = ClassUtil.obj2long(DateUtil.MINUTE); // 每分钟执行一次
        clearTimer.schedule(new TimerTask() {
            long i = 1;
            BlogList list = new BlogList();

            @Override
            public void run() {
                try {
                    BlogAdd();
                    System.out.println("执行了" + i + "次");
                    Thread.sleep(i);
                    i++;
                } catch (Exception e) {
                    log.error("执行出错 ", e);
                }
            }

            private void BlogAdd() {
                try {
                    for (String url : list.getList()) {
                        // 访问博客的方法
                        Connection.Response response = Jsoup.connect(url).timeout(2000).execute();
//                      System.out.println(response.parse().toString());
                        System.out.println(response.statusCode());
                    }

                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }, 10, period); // 从10毫秒后 每天执行一次
    }
}

class BlogList {
    List<String> list = new ArrayList<>();

    public BlogList() {
        list.add("https://blog.csdn.net/mengxiangxingdong/article/details/82415631");

    }

    public List<String> getList() {
        return list;
    }
}
